package kines.ct;

import kines.*;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.*;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class Phase extends Catalog implements SerializableDB {
  /**
   * Default contructor
   */
  public Phase( ){

  }
  /**
   * Contructor with identifier
   * @param phaseId DB Phase Identifier
   * @throws java.lang.Exception
   */
  private Phase( int phaseId )throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_phases = ConnRs.createStatement( );
    String str_phases = "select fase from fase where id_fase = "+phaseId;
    ResultSet rs_phases = stmt_phases.executeQuery( str_phases );
    if( rs_phases.next() ){
      this.id = phaseId;
      this.description = rs_phases.getString("fase");
    }
    ConnRs.close();
  }
  /**
   * Gets phase id
   * @param phase phase Name
   * @return phase Id
   * @throws java.lang.Exception
   */
  public static int getId( String phase )throws Exception{
    int idR;
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    Statement stmt_phase = ConnRs.createStatement( );
    String str_phase = "select id_fase from fase where fase = '"+(phase!=null?phase.trim():"")+"'";
    ResultSet rs_phase = stmt_phase.executeQuery( str_phase );
    if( rs_phase.next() ){
      idR = rs_phase.getInt("id_fase");
    }else{
      idR = -1;
    }
    ConnRs.close();
    return idR;
  }
  public void saveData( HttpSession session ) throws Exception{
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method saveData() not yet implemented.");
  }
  public boolean deleteData( ) throws Exception{
    /**@todo Implement this kines.SerializableDB method*/
    throw new java.lang.UnsupportedOperationException("Method saveData() not yet implemented.");
  }
  public static Object[] getData()throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    ArrayList phases = new ArrayList();
    Statement stmt_phases = ConnRs.createStatement( );
    String str_phases = "select id_fase,fase from fase";
    ResultSet rs_phases = stmt_phases.executeQuery( str_phases );
    while( rs_phases.next() ){
      Phase ph = new Phase( );
      ph.setId( rs_phases.getInt("id_fase") );
      ph.setDescription( rs_phases.getString("fase") );
      phases.add(ph);
    }
    ConnRs.close();
    return phases.toArray();
  }
  /**
   * Gets String with options to html combo
   * @throws Exception
   * @return String
   */
  public static String getHtmlCombo( int selected )throws Exception{
    Driver DriverRs = (Driver)Class.forName(driver).newInstance();
    Connection ConnRs = DriverManager.getConnection(strconn,username,password);
    String phasesHtmlCombo = "";
    Statement stmt_phases = ConnRs.createStatement( );
    String str_phases = "select id_fase,fase from fase";
    ResultSet rs_phases = stmt_phases.executeQuery( str_phases );
    while( rs_phases.next() ){
      int id_fase = rs_phases.getInt("id_fase");
      phasesHtmlCombo += "<option "+( id_fase==selected?"selected":"" )+" value = '"+id_fase+"'>"+rs_phases.getString("fase")+"</option>\n";
    }
    ConnRs.close();
    return phasesHtmlCombo;
  }
  public static Phase getData( int phaseId )throws Exception{
    return new Phase( phaseId );
  }
  public static Catalog[] getData(String where, String order) throws Exception{
    throw new java.lang.UnsupportedOperationException("Method getData() not yet implemented.");
  }
}
